import { Component, OnInit } from '@angular/core';
import { Router } from '@angular/router'; // 引入angular路由模块
import * as echarts from 'echarts';
@Component({
  selector: 'app-echart01',
  templateUrl: './echart01.component.html',
  styleUrls: ['./echart01.component.css']
})
export class Echart01Component implements OnInit {

  public showFunE1: boolean = false;
  public showFunE2: boolean = false;
  public showFunE3: boolean = false;
  public showFunE4: boolean = false;

  public toggleE2() {
    this.showFunE2 = !this.showFunE2;
  }
  // 在构造函数中注入Router
  constructor(private router: Router) { }

  ngOnInit() {
    this.drawEchart();
  }

  drawEchart() {
    // 基于准备好的dom，初始化echarts实例
    var myChart = echarts.init(document.getElementById('e1'));
    // 指定图表的配置项和数据
    var option = {
      title: {
        text: 'ECharts 入门示例1'
      },
      tooltip: {},
      legend: {
        data: ['销量']
      },
      xAxis: {
        data: ["衬衫", "羊毛衫", "雪纺衫", "裤子", "高跟鞋", "袜子"]
      },
      yAxis: {},
      series: [{
        name: '销量',
        type: 'bar',
        data: [5, 20, 36, 10, 10, 20]
      }]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart.setOption(option);

    // 基于准备好的dom，初始化echarts实例
    var myChart2 = echarts.init(document.getElementById('e2'));
    // 指定图表的配置项和数据
    var option2 = {
      title: {
        text: 'ECharts 入门示例2'
      },
      tooltip: {},
      legend: { // 若不手动设置，则会显示data中的5个name
        data: ['访问来源']
      },
      /*xAxis: {}, // 饼图不需要x和y轴，若不注释掉会显示两道黑边
      yAxis: {},*/
      series: [{
        name: '访问来源',
        type: 'pie',
        roseType: 'angle', // 设为南丁格尔图
        radius: '55%',
        data: [
          { value: 235, name: '视频广告' },
          { value: 274, name: '联盟广告' },
          { value: 310, name: '邮件营销' },
          { value: 335, name: '直接访问' },
          { value: 400, name: '搜索引擎' }
        ]
      }]
    };
    // 使用刚指定的配置项和数据显示图表。
    myChart2.setOption(option2);

    var base = +new Date(1968, 9, 3);
    var oneDay = 24 * 3600 * 1000;
    var date = [];

    var data = [Math.random() * 300];

    for (var i = 1; i < 20000; i++) {
      var now = new Date(base += oneDay);
      date.push([now.getFullYear(), now.getMonth() + 1, now.getDate()].join('/'));
      data.push(Math.round((Math.random() - 0.5) * 20 + data[i - 1]));
    }

    var option3 = {
      tooltip: {
        trigger: 'axis',
        position: function (pt) {
          return [pt[0], '10%'];
        }
      },
      title: {
        left: 'center',
        text: '大数据量面积图',
      },
      toolbox: {
        feature: {
          dataZoom: {
            yAxisIndex: 'none'
          },
          restore: {},
          saveAsImage: {}
        }
      },
      xAxis: {
        type: 'category',
        boundaryGap: false,
        data: date
      },
      yAxis: {
        type: 'value',
        boundaryGap: [0, '100%']
      },
      dataZoom: [{
        type: 'inside',
        start: 0,
        end: 10
      }, {
        start: 0,
        end: 10,
        handleIcon: 'M10.7,11.9v-1.3H9.3v1.3c-4.9,0.3-8.8,4.4-8.8,9.4c0,5,3.9,9.1,8.8,9.4v1.3h1.3v-1.3c4.9-0.3,8.8-4.4,8.8-9.4C19.5,16.3,15.6,12.2,10.7,11.9z M13.3,24.4H6.7V23h6.6V24.4z M13.3,19.6H6.7v-1.4h6.6V19.6z',
        handleSize: '80%',
        handleStyle: {
          color: '#fff',
          shadowBlur: 3,
          shadowColor: 'rgba(0, 0, 0, 0.6)',
          shadowOffsetX: 2,
          shadowOffsetY: 2
        }
      }],
      series: [
        {
          name: '模拟数据',
          type: 'line',
          smooth: true,
          symbol: 'none',
          sampling: 'average',
          itemStyle: {
            color: 'rgb(255, 70, 131)'
          },
          areaStyle: {
            color: new echarts.graphic.LinearGradient(0, 0, 0, 1, [{
              offset: 0,
              color: 'rgb(255, 158, 68)'
            }, {
              offset: 1,
              color: 'rgb(255, 70, 131)'
            }])
          },
          data: data
        }
      ]
    };

    var myChart3 = echarts.init(document.getElementById('e3'));
    myChart3.setOption(option3);

    var myChart4 = echarts.init(document.getElementById('e4'));
    // 模拟数据
    function getVirtulData(year) {
      year = year || '2018';
      var date = +echarts.number.parseDate(year + '-01-01');
      var end = +echarts.number.parseDate(year + '-12-31');
      var dayTime = 3600 * 24 * 1000;
      var data = [];
      for (var time = date; time <= end; time += dayTime) {
        data.push([
          echarts.format.formatTime('yyyy-MM-dd', time),
          Math.floor(Math.random() * 10000)
        ]);
      }
      return data;
    }
    var option4 = {
      visualMap: {
        show: false,
        min: 0,
        max: 10000
      },
      calendar: {
        range: '2018'
      },
      series: {
        type: 'heatmap',
        coordinateSystem: 'calendar',
        data: getVirtulData(2018)
      }
    };
    myChart4.setOption(option4);
  }

  public myMouseOver(over: boolean) {
    if(over)
      this.showFunE4 = true;
    else
      {
        let that = this;
        setTimeout(function(){
          if(that.hoverIn)
            that.showFunE4 = true;
          else
            that.showFunE4 = false;
        },1000);
      }
  }

  public hoverIn: boolean = false;
  public myHoverIn(hover: boolean) {
      this.hoverIn = hover;
  }

}

